<?xml version="1.0" encoding="UTF-8"?>
<sqlMap namespace="${namespace}" jdbcTimeout="5">

<#if data?? && data?has_content>
	<!-- 查询 -->
	<sql id="query">
	   	<![CDATA[   	
			SELECT
				<#assign selectFields = [] />
				<#list data as field>
					<#assign selectFields = selectFields + [ sns.format("\t\t\t\t{name} as \"{name}\"",field)] />
				</#list>
${sns.join(selectFields,",\n")}
			FROM
				${tablename}
			WHERE
				1 = 1
				<#assign searchFields = [] />
				<#list data as field>
					<#if field.issearch == '1'>
						<#if field.searchType == '0'>
				${sns.format("<#if {name}?? && {name}?has_content >and {name} = :{name}</#if>",field)}
						<#else>
						<#assign fuzzy=" like \"%${r'${'}"+field.name+"${r'}'}%\""/>
				${sns.format("<#if {name}?? && {name}?has_content >and {name}",field) + fuzzy+"</#if>"}
						</#if>
					</#if>
				</#list>
	   	]]>
	</sql>
	
	<!-- 删除 -->
	<sql id="delete">
	   	<![CDATA[   	
			DELETE 
			FROM
				${tablename}
			WHERE
				1 = 2
				<#assign deleteFields = snc.filter4include(data,'isdel','1') />
				<#if deleteFields?? && deleteFields?has_content>
				<#list deleteFields as field>
					<#if field.isdel == '1' >
				${sns.format("<#if {name}?? && {name}?has_content >and {name} = :{name}</#if>",field)}
					</#if>
				</#list>
				<#else>
				</#if>
				
	   	]]>
	</sql>
	
	<!-- 修改 -->
	<sql id="update">
	   	<![CDATA[   	
			UPDATE
				${tablename} 
			SET
				<#assign updatebleFields = snc.filter4include(data,'isupdate','1') />
				<#assign updateFields = [] />
				<#list updatebleFields as field>
					<#assign updateFields = updateFields + ["\t\t\t\t"+ sns.format("{name} = {pvalue}",field,{"pvalue":sns.show(field.upvalue,":"+field.name)})] />
				</#list>
${sns.join(updateFields,",\n")}
			WHERE
				<#assign primary = snc.filterObj(data,'isprimary','1') />
				<#if primary?exists && primary?has_content>
				${sns.format("{name} = :{name}",primary)}
				<#else>
					1=2
				</#if> 
	   	]]>
	</sql>

	<!-- 新增 -->
	<sql id="save">
		<![CDATA[   	
			INSERT INTO ${tablename}
				(
				<#assign insertbleFields = snc.filter4include(data,'isprimary','') />
				<#assign insertFields = [] />
				<#list insertbleFields as field>
					<#assign insertFields = insertFields + ["\t\t\t\t"+ field.name ] />
				</#list>
${sns.join(insertFields,",\n")}
				) 
			VALUES
				(
				<#assign insertFields = [] />
				<#list insertbleFields as field>
					<#assign insertFields = insertFields + ["\t\t\t\t"+ sns.format("{pvalue}",field,{"pvalue":sns.show(field.dvalue,":"+field.name)}) ] />
				</#list>
${sns.join(insertFields,",\n")}
				)
		]]>
	</sql>

</#if>

</sqlMap>